<template>
  <section>
    <!--工具栏：关键字查询 + 添加-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表：分页查询 -->
    <el-table :data="dictionaryTypes" highlight-current-row v-loading="listLoading"
              @row-dblclick="dictionary" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="50">
      </el-table-column>
      <el-table-column align="center" prop="name" label="类型名称" width="150">
      </el-table-column>
      <el-table-column align="center" prop="sn" label="类型编号" width="150">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具栏：批量删除 + 分页栏-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals" :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>
    <!--
       el-dialog - 对话框
          :title="title" - 标题
          :visible.sync="saveFormVisible" - 控制对话框是都显示，默认为false。点击添加或修改时改成true，就可以显示对话框了
          :close-on-click-modal="false" - 点对话框外不关闭对话框
      el-form - 表单
          :model="saveForm" - 表单数据/模型数据
          label-width="80px" - 标签的宽度
          :rules="saveFormRules" - 表单项验证
          ref="saveForm" - 表单验证，只有表单项每一项都通过，才能提交数据
      el-form-item - 表单项
          label="部门名称" - 标签
          prop="name" - 需要验证就要写prop
    -->
    <!--添加或编辑对话框-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="100px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="类型名称" prop="name" style="width: 400px">
          <el-input v-model="saveForm.name"></el-input>
        </el-form-item>
        <el-form-item label="类型编号" prop="sn" style="width: 400px">
          <el-input v-model="saveForm.sn"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--=========数据字典明细============-->
    <el-dialog title="数据字典明细" :visible.sync="dictionaryModal"
               :before-close="handleClose" :close-on-click-modal="false">
      <!--工具栏：关键字查询 + 添加-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true">
          <el-form-item>
            <el-input v-model="keyword_d" placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="keywordQuery_d">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleAdd_d">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--列表：分页查询 -->
      <el-table :data="dictionarys_d" highlight-current-row v-loading="listLoading_d"
                @selection-change="selsChange_d" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column align="center" prop="name" label="明细名称" width="200">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template scope="scope">
            <el-button size="small" @click="handleEdit_d(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDel_d(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--工具栏：批量删除 + 分页栏-->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove_d" :disabled="this.sels_d.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange_d"
                       :page-size="pageSize_d" :total="totals_d" :current-page="currentPage_d"
                       style="float:right;">
        </el-pagination>
      </el-col><br/><br/><br/>
    </el-dialog>

    <!--
       el-dialog - 对话框
          :title="title" - 标题
          :visible.sync="saveFormVisible" - 控制对话框是都显示，默认为false。点击添加或修改时改成true，就可以显示对话框了
          :close-on-click-modal="false" - 点对话框外不关闭对话框
      el-form - 表单
          :model="saveForm" - 表单数据/模型数据
          label-width="80px" - 标签的宽度
          :rules="saveFormRules" - 表单项验证
          ref="saveForm" - 表单验证，只有表单项每一项都通过，才能提交数据
      el-form-item - 表单项
          label="部门名称" - 标签
          prop="name" - 需要验证就要写prop
    -->
    <!--添加或编辑对话框-->
    <el-dialog :title="title_d" :visible.sync="saveFormVisible_d" :close-on-click-modal="false">
      <el-form :model="saveForm_d" label-width="100px" :rules="saveFormRules_d" ref="saveForm_d">
        <el-form-item label="明细名称" prop="name" style="width: 400px">
          <el-input v-model="saveForm_d.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible_d = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit_d" :loading="saveLoading_d">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      //1.分页查询的相关数据
      dictionaryTypes: [],    //列表数据
      listLoading: false, //列表的加载框
      pageSize: 5,        //每页显示条数
      currentPage: 1,     //当前页
      totals: 0,          //总条数

      //2.高级查询相关数据
      keyword: '',

      //3.批量删除的相关数据
      sels: [],//列表选中列


      //4.添加或修改的相关数据
      title: '',          //标题
      saveFormVisible: false,//控制对话框是都显示，默认为false
      saveLoading: false, //加载框
      saveFormRules: { // 表单项验证
        name: [
          {required: true, message: '请输入类型名称', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入类型编号', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      saveForm: {
        id: null,
        sn: "",
        name: ""
      },

      //===========数据字典明细======================
      //1.分页查询的相关数据
      dictionarys_d: [],    //列表数据
      listLoading_d: false, //列表的加载框
      pageSize_d: 5,        //每页显示条数
      currentPage_d: 1,     //当前页
      totals_d: 0,          //总条数

      //2.高级查询相关数据
      keyword_d: '',

      //3.批量删除的相关数据
      sels_d: [],//列表选中列


      //4.添加或修改的相关数据
      title_d: '',          //标题
      saveFormVisible_d: false,//控制对话框是都显示，默认为false
      saveLoading_d: false, //加载框
      saveFormRules_d: { // 表单项验证
        name: [
          {required: true, message: '请输入类型名称', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      saveForm_d: {
        id: null,
        name: "",
        types_id: this.type_id
      },
      type_id: null,
      dictionaryModal: false,
    }
  },
  methods: {

    //1.分页查询 - 获取部门列表
    getDictionaryTypes() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;//显示加载框
      this.$http.post("/dictionaryType",para).then(res=>{
        //注意：res.data是axios获取响应数据的固定语法，这个实际就是PageList对象
        this.dictionaryTypes = res.data.data;//显示当前页分页数据
        this.totals = res.data.totals;//计算分页栏
        this.listLoading = false;//关闭加载框
      }).catch(res=>{
        this.$message.error("系统繁忙，请稍后重试!!!【400,404】")
      })
    },

    //2.点击分页实现
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getDictionaryTypes();
    },

    //3.关键字查询
    keywordQuery(){
      //假如当前页不在第一页，防止高级查询没有那么多页的时候出现空数据的问题
      this.currentPage = 1;
      this.getDictionaryTypes();
    },

    //4.删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => { //点确认框的确认按钮
        this.listLoading = true;
        this.$http.delete("/dictionaryType/"+row.id).then(res=>{
          if(res.data.success){
            //处理当前页
            //算总页数
            let totalPage = Math.ceil(this.totals/this.pageSize);
            //当删除最后一页的最后一条数据，并且不是第一页，就让当前页-1
            if(this.currentPage==totalPage && (this.totals-1)%this.pageSize==0 && this.currentPage!=1){
              this.currentPage = this.currentPage - 1;
            }

            //方式二：
            /*if(this.dictionaryTypes.length==1 && this.currentPage!=1){
              this.currentPage = this.currentPage - 1;
            }*/

            this.getDictionaryTypes();
            this.$message.success("删除成功！");
          }else{
            this.$message.error("系统繁忙，请稍后重试!!!【500】");
          }
          this.listLoading = false;
        }).catch(res=>{
          this.$message.error("系统繁忙，请稍后重试!!!【400,404】");
        })
      }).catch(() => { //点取消
      });
    },

    //5.选项改变时触发
    selsChange: function (sels) {
      //将选中的所有对象【集合】赋值给模型数据sels
      this.sels = sels;
    },

    //6.批量删除
    batchRemove: function () {
      //获取选中对象的id
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/dictionaryType",ids).then(res=>{
          if(res.data.success){

            //处理当前页
            //算总页数
            let totalPage = Math.ceil(this.totals/this.pageSize);
            //当删除最后一页的所有数据，并且不是第一页，就让当前页-1
            if(this.currentPage==totalPage && (this.totals-this.sels.length)%this.pageSize==0 && this.currentPage!=1){
              this.currentPage = this.currentPage - 1;
            }

            this.getDictionaryTypes();
            this.$message.success("删除成功！");
          }else{
            this.$message.error("系统繁忙，请稍后重试!!!【500】");
          }
        }).catch(res=>{
          this.$message.error("系统繁忙，请稍后重试!!!【400,404】");
        })
      }).catch(() => {
      });
    },

    //7.显示编辑界面
    handleEdit: function (index, row) {
      this.title = " 编 辑 "
      //防止多个引用指向同一个对象
      this.saveForm = Object.assign({}, row);
      //显示对话框
      this.saveFormVisible = true;
    },
    //8.显示新增界面
    handleAdd: function () {
      this.title = " 新 增 "
      //重置
      this.saveForm = {
        id: null,
        sn: "",
        name: ""
      };
      this.saveFormVisible = true;
    },
    //9.提交数据
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {//表单验证
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            this.$http.put("/dictionaryType",this.saveForm).then(res=>{
              if(res.data.success){
                this.getDictionaryTypes();
                this.$message.success("操作成功！");
              }else{
                this.$message.error("系统繁忙，请稍后重试!!!【500】");
              }
              this.saveLoading = false; //关闭加载框
              this.saveFormVisible = false;//关闭对话框
            }).catch(res=>{
              this.$message.error("系统繁忙，请稍后重试!!!【400,404】");
            })
          });
        }
      });
    },

    //=========数据字典内容==============================
    //双击某一行弹出数据
    dictionary: function(row){
      //展示第一页数据
      this.currentPage_d = 1;
      //打开模态框
      this.dictionaryModal = true;
      this.type_id = row.id;
      this.getDictionarys_d();
    },

    //退出明细后清空高级查询的内容
    handleClose(empty) {
      empty();
      this.keyword_d='';
    },

    //1.分页查询 - 获取部门列表
    getDictionarys_d() {
      let para = {
        currentPage_d: this.currentPage_d,
        pageSize_d: this.pageSize_d,
        type_id: this.type_id,
        keyword: this.keyword_d
      };
      this.listLoading_d = true;//显示加载框
      this.$http.post("/dictionary",para).then(res=>{
        //注意：res.data是axios获取响应数据的固定语法，这个实际就是PageList对象
        this.dictionarys_d = res.data.data;//显示当前页分页数据
        this.totals_d = res.data.totals;//计算分页栏
        this.listLoading_d = false;//关闭加载框
      }).catch(res=>{
        this.$message.error("系统繁忙，请稍后重试!!!【400,404】")
      })
    },

    //2.点击分页实现
    handleCurrentChange_d(currentPage_d) {
      this.currentPage_d = currentPage_d;
      this.getDictionarys_d();
    },

    //3.关键字查询
    keywordQuery_d(){
      //假如当前页不在第一页，防止高级查询没有那么多页的时候出现空数据的问题
      this.currentPage_d = 1;
      this.getDictionarys_d();
    },

    //4.删除
    handleDel_d: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => { //点确认框的确认按钮
        this.listLoading_d = true;
        this.$http.delete("/dictionary/"+row.id).then(res=>{
          if(res.data.success){
            //处理当前页
            //算总页数
            let totalPage = Math.ceil(this.totals_d/this.pageSize_d);
            //当删除最后一页的最后一条数据，并且不是第一页，就让当前页-1
            if(this.currentPage_d==totalPage && (this.totals_d-1)%this.pageSize_d==0 && this.currentPage_d!=1){
              this.currentPage_d = this.currentPage_d - 1;
            }

            //方式二：
            /*if(this.dictionarys_d.length==1 && this.currentPage_d!=1){
              this.currentPage_d = this.currentPage_d - 1;
            }*/

            this.getDictionarys_d();
            this.$message.success("删除成功！");
          }else{
            this.$message.error("系统繁忙，请稍后重试!!!【500】");
          }
          this.listLoading_d = false;
        }).catch(res=>{
          this.$message.error("系统繁忙，请稍后重试!!!【400,404】");
        })
      }).catch(() => { //点取消
      });
    },

    //5.选项改变时触发
    selsChange_d: function (sels_d) {
      //将选中的所有对象【集合】赋值给模型数据sels_d
      this.sels_d = sels_d;
    },

    //6.批量删除
    batchRemove_d: function () {
      //获取选中对象的id
      var ids = this.sels_d.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading_d = true;
        this.$http.patch("/dictionary",ids).then(res=>{
          if(res.data.success){

            //处理当前页
            //算总页数
            let totalPage = Math.ceil(this.totals_d/this.pageSize_d);
            //当删除最后一页的所有数据，并且不是第一页，就让当前页-1
            if(this.currentPage_d==totalPage && (this.totals_d-this.sels_d.length)%this.pageSize_d==0 && this.currentPage_d!=1){
              this.currentPage_d = this.currentPage_d - 1;
            }

            this.getDictionarys_d();
            this.$message.success("删除成功！");
          }else{
            this.$message.error("系统繁忙，请稍后重试!!!【500】");
          }
        }).catch(res=>{
          this.$message.error("系统繁忙，请稍后重试!!!【400,404】");
        })
      }).catch(() => {
      });
    },

    //7.显示编辑界面
    handleEdit_d: function (index, row) {
      this.title_d = " 编 辑 "
      //防止多个引用指向同一个对象
      this.saveForm_d = Object.assign({}, row);
      //显示对话框
      this.saveFormVisible_d = true;
    },
    //8.显示新增界面
    handleAdd_d: function () {
      this.title_d = " 新 增 "
      //重置
      this.saveForm_d = {
        id: null,
        name: "",
        types_id: this.type_id
      };
      this.saveFormVisible_d = true;
    },
    //9.提交数据
    saveSubmit_d: function () {
      this.$refs.saveForm_d.validate((valid) => {//表单验证
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading_d = true;
            this.$http.put("/dictionary",this.saveForm_d).then(res=>{
              if(res.data.success){
                this.getDictionarys_d();
                this.$message.success("操作成功！");
              }else{
                this.$message.error("系统繁忙，请稍后重试!!!【500】");
              }
              this.saveLoading_d = false; //关闭加载框
              this.saveFormVisible_d = false;//关闭对话框
            }).catch(res=>{
              this.$message.error("系统繁忙，请稍后重试!!!【400,404】");
            })
          });
        }
      });
    },
  },
  mounted() {
    this.getDictionaryTypes();
    this.getDictionarys_d();
  }
}

</script>

<style scoped>
</style>